package com.example.lin.zyfypt_327ljd.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.example.lin.zyfypt_327ljd.R;
import com.example.lin.zyfypt_327ljd.adapter.FocusAdapter;
import com.example.lin.zyfypt_327ljd.bean.FocusBean;
import com.example.lin.zyfypt_327ljd.bean.UserBean;
import com.example.lin.zyfypt_327ljd.iface.FocusListListener;
import com.example.lin.zyfypt_327ljd.model.FocusListModel;

import java.util.List;

public class FocusActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;//显示布局效果
    private FocusAdapter fAdapter;//适配器
    private List<FocusBean<UserBean>>list=null;//数据源
    private SharedPreferences sp;
    private String sessionid="";
    private int page=1;// 代表页数，并初始化为1，代表第1页。
    private int lastVisibleItemPosition;//最后一条可见条目的位置

    private FocusListListener<UserBean> focusListListener=new FocusListListener<UserBean>() {

        @Override
        public void onResponse(List<FocusBean<UserBean>> beanlist) {
            if(page==1)
            {
                list=beanlist;
            }
            else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            fAdapter.setList(list);//传给adapter
            fAdapter.notifyDataSetChanged();//通知更新
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(FocusActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_focus);
        sp = this.getSharedPreferences("login", MODE_PRIVATE);
        readSP();
        System.out.println("----onCreateView page=" + page);
        initRecyclerView();
        //实例化ArticleModel，调用方法获取网络数据
        FocusListModel focusListModel = new FocusListModel();
        focusListModel.getResultList("user", page,sessionid,focusListListener);
    }
    private void readSP(){
        sessionid=sp.getString("sessionid",null);
        System.out.print(sessionid);
    }

    private void initRecyclerView() {
        fAdapter = new FocusAdapter(this);
        //获取RecyclerView,设置属性，获取数据源，绑定
        recyclerView = (RecyclerView)findViewById(R.id.recyclerview_f);
        //创建默认线性布局
        layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item高度，提高性能
        recyclerView.setHasFixedSize(true);
        //获取数据并创建Adapter
        //fAdapter.setList(list);
        //绑定RecyclerView和adapter
        recyclerView.setAdapter(fAdapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page += 1;
                    //再次实例化ArticleModel，调用方法获取网络数据，请求新一页数据
                    FocusListModel focusListModel = new FocusListModel();
                    focusListModel.getResultList("user", page, sessionid, focusListListener);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位置
            }
        });

    }
}
